﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace DAMS.Models
{
    /// <summary>
    /// 分类类型
    /// </summary>
    public enum CategoryType
    {
        /// <summary>
        /// 顶层分类：系统预设，新建分类无法设置此分类类型
        /// </summary>
        [Display(Name ="顶层")]
        Top=1,
        /// <summary>
        /// 首页：系统预设，新建分类无法设置此分类类型
        /// </summary>
        [Display(Name = "首页")]
        Home = 2,
        /// <summary>
        /// 分类
        /// </summary>
        [Display(Name ="分类")]
        Category=4,
        /// <summary>
        /// 单页
        /// </summary>
        [Display(Name = "单页")]
        SinglePage=8,
        /// <summary>
        /// 链接
        /// </summary>
        [Display(Name = "链接")]
        Url=16
        
        
    }

    /// <summary>
    /// 动态属性转换约定
    /// </summary>
    /// <remarks>
    /// 处理场景 树形结构数据 后台代码实体定义 为 Id Childrens 但是前台树形控件所需数据结构为  id,nodes
    /// 这个时候可以使用该属性约定转换类 动态设置 序列化后字段名称
    /// </remarks>
    /// <example>
    ///     JsonSerializerSettings setting = new JsonSerializerSettings();
    ///     setting.ContractResolver = new PropsContractResolver(new Dictionary<string, string> { { "Id", "id" }, { "Text", "text" }, { "Childrens", "nodes" } });
    ///     string AA = JsonConvert.SerializeObject(cc, Formatting.Indented, setting);
    /// </example>
    public class PropsContractResolver : DefaultContractResolver
    {
        Dictionary<string, string> dict_props = null;
        bool retain;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dictPropertyName">键值对，key为属性值，value为输出值</param>
        /// <param name="_retain"></param>
        public PropsContractResolver(Dictionary<string, string> dictPropertyName,bool _retain=true)
        {
            //指定字段要序列化成什么名称
            this.dict_props = dictPropertyName;
            this.retain = _retain;
        }

        protected override IList<JsonProperty> CreateProperties(System.Type type,MemberSerialization memberSerialization)
        {
            IList<JsonProperty> list =
            base.CreateProperties(type, memberSerialization);
            //只保留清单有列出的属性
            return list.Where(p => {
                if (retain)
                {
                    return dict_props.Values.Contains(p.PropertyName);
                }
                else
                {
                    return !dict_props.Values.Contains(p.PropertyName);
                }
            }).ToList();
        }
        protected override string ResolvePropertyName(string propertyName)
        {
            string newPropertyName = string.Empty;
            if (dict_props != null && dict_props.TryGetValue(propertyName, out newPropertyName))
            {
                return newPropertyName;
            }
            else
            {
                return base.ResolvePropertyName(propertyName);
            }
        }
    }
}

namespace DAMS.Models
{
    /// <summary>
    /// 分类显示类型
    /// </summary>
    public enum CategoryShowType
    {
        /// <summary>
        /// 不显示在任何位置
        /// </summary>
        [Display(Name ="不显示")]
        None =1,
        /// <summary>
        /// 显示在首页顶部导航栏
        /// </summary>
        [Display(Name = "首页主导航栏")]
        ShowHomeTop =2,
        /// <summary>
        /// 显示在首页侧导航栏
        /// </summary>
        [Display(Name = "首页侧边")]
        ShowHomeSide =4,
        /// <summary>
        /// 显示在首页底部导航栏
        /// </summary>
        [Display(Name = "首页底部")]
        ShowHomeFoot =8,
        /// <summary>
        /// 显示在分类列表侧导航栏
        /// </summary>
        [Display(Name = "列表页侧边")]
        ShowListSide =16,
        /// <summary>
        /// 显示在内容页侧栏
        /// </summary>
        [Display(Name = "内容页侧边")]
        ShowContentSide =32
    }
}